/**
 * Copyright 2010 Victor Egea Hernando
 *
 * RockScript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 * 
 * RockScript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with RockScript.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef VM_MACROS_H_
#define VM_MACROS_H_

// register offset
#define REG16_OFFSET( IDX ) \
    ( -( (corelib::int32)( (( _aroutine->_useful_sf + IDX ) % _aroutine->_useful_sf ) + 1 ) ) )

// opcode operands 
#define OPA ( _ci._inst._a )
#define OPB ( _ci._inst._rest._bc._b )
#define OPC ( _ci._inst._rest._bc._c )
#define OPZ ( _ci._inst._rest._z )

// Indexations
#define INDXA( REGEXP ) _aroutine->_ep[ REGEXP ]
#define INDXZ( REGEXP ) _aroutine->_sp[ REGEXP ]

// routine registers
#define RA INDXA( OPA )
#define RB INDXA( OPB )
#define RC INDXA( OPC )
#define RZ INDXZ( REG16_OFFSET(OPZ) )

// return regiters
#define RETREG  _aroutine->_ep[ SP_ARGUMENTS_THIS ]
#define LRETREG _aroutine->_stack[ 0 ]

#define STATE ( _aroutine->_state )
#define ARG _aroutine->_sp[ SP_CONTEXT_REGS + OPA ]

// method attributes
#define OPCODES _aroutine->_mp->_opcode
#define CONSTANTS _aroutine->_mp->_constant

// rise an error
#define C_M_ERROR(CLASS, METHOD, STR) { \
	_hnd->dynError( vm_error( _aroutine->_id, CLASS, METHOD, STR ) ); \
	return( false ); \
}

// set jump flag
#define SET_JUMP_FLAG( CEXP ) \
	if( CEXP ){ \
		FLAGS_SET( _aroutine->_flags, RFLG_JUMP ); \
	}else{ \
		FLAGS_OFF( _aroutine->_flags, RFLG_JUMP ); \
	}

#endif // VM_MACROS_H_

